/*
 * Copyright (c) 2024 - 2025 Midea Company of Midea Group.
 *
 * File Name 	    : font_manager.h
 * Description	    : 
 * Author			: huocy
 * Date	            : 2024/02/26
 * History	        : 
 */


#include <stdlib.h>
#include "font_manager.h"
#include "src/misc/lv_ll.h"
#include <string.h>
#include <stdio.h>
// #include "ms_osal_log.h"
#include "../../pt_printk.h"

#if LV_USE_FREETYPE

static lv_ll_t font_list;

#if CONFIG_SOC_BK7258
#define TMP_MIDEA_RES_PATH_PREFIX "/spi/res/"
#else
#define TMP_MIDEA_RES_PATH_PREFIX "./build/output/install/etc/midea_gw/ui_service/res/"
#endif
#define MIDEA_MAX_PATH_LEN 256

int init_font_manager(void)
{
	_lv_ll_init(&font_list, sizeof(font_info_obj));
    return 0;
}

void get_fontfile_path(char* path, int pathLen, char* name)
{ 
	memset(path, 0, pathLen);
    ///备注 与mac系统区别
    // sprintf()
	// lv_snprintf(path, pathLen, "%s/font/%s", RES_PATH_PREFIX);
    // printf("get_fontfile_path RES_PATH_PREFIX\n");
	snprintf(path, pathLen, "%s%s", TMP_MIDEA_RES_PATH_PREFIX, name);
   
}

static font_info_obj* find_font_info_obj(FONT_TYPE type, uint16_t weight, uint16_t style)
{
    font_info_obj* font_obj = _lv_ll_get_head(&font_list);
    while (font_obj)
    {
        if ((font_obj->type == type)
            && (font_obj->weight == weight)
            && (font_obj->style == style))
        {
            return font_obj;
        }

        font_obj = _lv_ll_get_next(&font_list, font_obj);
    }

    return NULL;
}

static font_info_obj* find_font_info_obj_2(lv_font_t* font)
{
    font_info_obj* font_obj = _lv_ll_get_head(&font_list);
    while (font_obj)
    {
        if (font_obj->font_info->font == font)
        {
            return font_obj;
        }

        font_obj = _lv_ll_get_next(&font_list, font_obj);
    }
    return NULL;
}

static font_info_obj* create_font_info_obj(FONT_TYPE type, uint16_t weight, uint16_t style)
{
    char font_file[MIDEA_MAX_PATH_LEN];
    switch (type)
    {
    case FONT_TYPE_CN:
        // get_fontfile_path(font_file, MIDEA_MAX_PATH_LEN, "font/XinH_XiJW.TTF");
        get_fontfile_path(font_file, MIDEA_MAX_PATH_LEN, "font/MideaType-Regular.ttf");
        break;
	case FONT_TYPE_LETTER:
		get_fontfile_path(font_file, MIDEA_MAX_PATH_LEN, "font/COLMO-Bold.otf");
        break;
	case FONT_TYPE_DIGIT:
		get_fontfile_path(font_file, MIDEA_MAX_PATH_LEN, "font/XinH_XiJW.TTF");
        break;
    case FONT_TYPE_BOLD:
		get_fontfile_path(font_file, MIDEA_MAX_PATH_LEN, "font/COLMO-Bold.otf");
        break;
    case FONT_TYPE_ICON:
		get_fontfile_path(font_file, MIDEA_MAX_PATH_LEN, "font/icomoon.ttf");
        break;
    default:
        break;
    }
    lv_ft_info_t* font_info = malloc(sizeof(lv_ft_info_t));
    ///需要改成配置的字体信息
    font_info->name = font_file;
    font_info->weight = weight;
    font_info->style = style;
    font_info->mem = NULL;
    font_info->mem_size = 0;
    font_info->name = font_file;
  
    if (!lv_ft_font_init(font_info))
    {
        printf("create font_info(%d %d %d) failed", type, weight, style);
    }
     
    font_info_obj* new_font_obj = _lv_ll_ins_tail(&font_list);
    new_font_obj->type = type;
    new_font_obj->weight = weight;
    new_font_obj->style = font_info->style;
    new_font_obj->font_info = font_info;
    new_font_obj->refer = 0;
   
    return new_font_obj;
}

lv_font_t* find_font(FONT_TYPE type, uint16_t weight)
{
    font_info_obj* font_obj = find_font_info_obj(type, weight, FT_FONT_STYLE_NORMAL);
    if (font_obj != NULL)
    {
        return font_obj->font_info->font;
    }

    font_info_obj* new_font_obj = NULL;
    if (weight <= 40)
    {
        new_font_obj = create_font_info_obj(type, weight, FT_FONT_STYLE_NORMAL);
    }
    else
    {
        new_font_obj = create_font_info_obj(type, weight, FT_FONT_STYLE_NORMAL);
    }

    return new_font_obj->font_info->font;
}

lv_font_t* find_font_style(FONT_TYPE type, uint16_t weight, uint16_t style)
{
    font_info_obj* font_obj = find_font_info_obj(type, weight, style);
    if (font_obj != NULL)
    {
        return font_obj->font_info->font;
    }

    font_info_obj* new_font_obj = create_font_info_obj(type, weight, style);
    return new_font_obj->font_info->font;
}

void inc_font_refer(lv_font_t* font)
{
    font_info_obj* font_obj = find_font_info_obj_2(font);
    if (font_obj != NULL)
    {
        font_obj->refer++;
    }
}

void dec_font_refer(lv_font_t* font)
{
    font_info_obj* font_obj = find_font_info_obj_2(font);
    if (font_obj != NULL)
    {
        font_obj->refer--;
        if (font_obj->refer == 0)
        {
            _lv_ll_remove(&font_list, font_obj);           
            lv_mem_free(font_obj);
        }
    }
}

void clean_font_cach(void)
{
    font_info_obj* font_obj;
    font_info_obj* font_obj_next;

    font_obj = _lv_ll_get_head(&font_list);
    font_obj_next = NULL;

    while (font_obj != NULL)
    {
        font_obj_next = _lv_ll_get_next(&font_list, font_obj);
        if (font_obj->refer == 0)
        {
            lv_ft_font_destroy(font_obj->font_info->font);
            _lv_ll_remove(&font_list, font_obj);           
            lv_mem_free(font_obj);
        }
        font_obj = font_obj_next;
    }
}

#endif